<?php

 


require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/Util/Filter.php';
require_once 'PHPUnit/Util/TestDox/NamePrettifier.php';
require_once 'PHPUnit/Util/Printer.php';

PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');


abstract class PHPUnit_Util_TestDox_ResultPrinter extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
{
    
    protected $prettifier;

    
    protected $testClass = '';

    
    protected $testFailed = FALSE;

    
    protected $tests = array();

    
    public function __construct($out = NULL)
    {
        parent::__construct($out);

        $this->prettifier = new PHPUnit_Util_TestDox_NamePrettifier;
        $this->startRun();
    }

    
    public function flush()
    {
        $this->doEndClass();
        $this->endRun();

        parent::flush();
    }

    
    public static function factory($type, $out = NULL)
    {
        $class = 'PHPUnit_Util_TestDox_ResultPrinter_' . $type;
        return new $class($out);
    }

    
    public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
    {
        $this->testFailed = TRUE;
    }

    
    public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
    {
        $this->testFailed = TRUE;
    }

    
    public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
    {
        $this->testFailed = TRUE;
    }

    
    public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
    {
        $this->testFailed = TRUE;
    }

    
    public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
    {
    }

    
    public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
    {
    }

    
    public function startTest(PHPUnit_Framework_Test $test)
    {
        $class = get_class($test);

        if ($this->testClass != $class) {
            if ($this->testClass != '') {
                $this->doEndClass();
            }

            $this->startClass($this->prettifier->prettifyTestClass($class));

            $this->testClass = $class;
            $this->tests     = array();
        }

        $this->testFailed = FALSE;
    }

    
    public function endTest(PHPUnit_Framework_Test $test, $time)
    {
        $prettifiedName = $this->prettifier->prettifyTestMethod($test->getName());

        if (!isset($this->tests[$prettifiedName])) {
            if (!$this->testFailed) {
                $this->tests[$prettifiedName]['success'] = 1;
                $this->tests[$prettifiedName]['failure'] = 0;
            } else {
                $this->tests[$prettifiedName]['success'] = 0;
                $this->tests[$prettifiedName]['failure'] = 1;
            }
        } else {
            if (!$this->testFailed) {
                $this->tests[$prettifiedName]['success']++;
            } else {
                $this->tests[$prettifiedName]['failure']++;
            }
        }
    }

    
    private function doEndClass()
    {
        foreach ($this->tests as $name => $data) {
            if ($data['failure'] == 0) {
                $this->onTest($name);
            }
        }

        $this->endClass($this->prettifier->prettifyTestClass($this->testClass));
    }

    
    protected function startRun()
    {
    }

    
    abstract protected function startClass($name);

    
    abstract protected function onTest($name);

    
    abstract protected function endClass($name);

    
    protected function endRun()
    {
    }
}

require_once 'PHPUnit/Util/TestDox/ResultPrinter/HTML.php';
require_once 'PHPUnit/Util/TestDox/ResultPrinter/Text.php';
?>
